//
//  USShareUsViewController.m
//  us
//
//  Created by karry on 2025/4/24.
//

#import "USShareUsViewController.h"
#import "USImageActivityItem.h"

@interface USShareUsViewController ()

@property (nonatomic) NSInteger type;

@end

@implementation USShareUsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"分享卡通形象";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ic_close 1"] style:UIBarButtonItemStylePlain target:self action:@selector(closeAction)];

    self.contentV.backgroundColor = [UIColor mi_1_color];
    [self.contentV jk_shadowWithColor:UIColor.lightGrayColor offset:CGSizeMake(3, 3) opacity:0.2 radius:3];

    
    UIButton *btn0 = [UIButton us_fillButtonWithTitle:@"下载"];
    [btn0 addTarget:self action:@selector(downLoadAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn0];
    
    UIButton *btn1 = [UIButton us_fillButtonWithTitle:@"分享"];
    [btn1 addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    [btn0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.height.mas_equalTo(UIButton.us_buttonDefaultHeight);
        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).mas_offset(-20);
    }];
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(btn0.mas_right).mas_offset(20);
        make.right.mas_offset(-20);
        make.width.height.centerY.mas_equalTo(btn0);
        
    }];
    
//    UIButton *sureBtn = [UIButton us_fillButtonWithTitle:@"分享"];
//    [sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:sureBtn];
//    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_offset(20);
//        make.right.mas_offset(-20);
//        make.height.mas_equalTo([UIButton us_buttonDefaultHeight]);
//        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).mas_offset(-12);
//    }];
    
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"ta",@"我",@"我们"]];
    seg.selectedSegmentTintColor = [UIColor mi_1_color];
    [seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:seg];
    [seg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.bottom.mas_equalTo(btn1.mas_top).mas_offset(-16);
    }];
    
    seg.selectedSegmentIndex = 2;
    
    self.type = 2;
    
}

- (void)closeAction{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)segAction:(UISegmentedControl *)sender{
    self.type = sender.selectedSegmentIndex;
    [USFeedbackTool selectionChanged];
}

- (void)setType:(NSInteger)type{
    _type = type;
    
    if (type == 0) {
        self.subV0.hidden = YES;
        self.subV1.hidden = YES;
        self.CV.hidden = NO;
        self.CV.character = [USUser user].relCharacter;
        self.infoLab.text = [NSString stringWithFormat:@"@%@",[USUser user].relUser.nickname];
        
    }else if (type == 1){
        
        self.subV0.hidden = YES;
        self.subV1.hidden = YES;
        self.CV.hidden = NO;
        self.CV.character = [USUser user].character;
        self.infoLab.text = [NSString stringWithFormat:@"@%@",[USUser user].nickname];
    }else{
        
        self.subV0.hidden = NO;
        self.subV1.hidden = NO;
        self.subV0.character = [USUser user].character;
        self.subV1.character = [USUser user].relCharacter;
        self.CV.hidden = YES;
        self.infoLab.text = [NSString stringWithFormat:@"@%@ & %@",[USUser user].nickname,USUser.user.relUser.nickname];
    }
    
    
}
- (void)downLoadAction:(UIButton *)sender{
    [USFeedbackTool impactSoft];
    
    UIImage *image = [self.contentV jk_screenshot];
    if (image) {
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }
    
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"保存失败: %@", error.localizedDescription);
        [self.view makeToast:@"保存失败"];
    } else {
        NSLog(@"保存成功");
        [self.view makeToast:@"已保存到相册"];
    }
}

- (void)shareAction:(id)sender{
    
    [USFeedbackTool impactSoft];

    UIImage *image = [self.contentV jk_screenshot];
    if (!image) return;
    
    USImageActivityItem *item = [[USImageActivityItem alloc]initWithImage:image];
    item.title = @"卡通形象";
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[item] applicationActivities:nil];
    activityVC.popoverPresentationController.sourceView = sender;
    [self presentViewController:activityVC animated:YES completion:nil];
    
}




@end
